package com.kaifamiao.strings;

/**
 * 从字符串中截取单个字符或子串
 * 1、char charAt( int index )
 * 2、String substring( int begin )
 * 3、String substring( int begin, int end )
 * 4、CharSequence subSequence( int begin, int end )
 */
public class CaptureString {
    public static void main(String[] args) {

        final String s = "http://www.kaifamiao.com/2022/kai/fa/miao";

        for (int i = 0, n = s.length(); i < n ; i++) {
            // 从字符串中获取指定下标的单个字符
            char ch = s.charAt( i );
            System.out.print( ch + " " );
        }
        System.out.println();

        int first = s.indexOf( 'a' );
        int last = s.lastIndexOf( 'a' );

        int begin = s.indexOf( '/', first );
        int end = s.lastIndexOf( '/' , last );

        // 从字符串中截取 [ begin, end ) 之间的子串并返回一个新的String实例
        String sub = s.substring( begin, end );
        System.out.println( sub );

        // 从字符串中截取 [ begin, end ) 之间的子串并返回一个新的CharSequence实例
        CharSequence cs = s.subSequence( begin, end );
        System.out.println( cs );

        // 从字符串中截取 [ begin, s.length() ) 之间的子串并返回一个新的String实例
        String x = s.substring( begin );
        System.out.println( x );

    }
}
